/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import database.DBAnggota;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.AnggotaBean;

/**
 *
 * @author elfatah
 */
public class AnggotaServlet extends HttpServlet {

    AnggotaBean bean = new AnggotaBean();
    DBAnggota dbobject = new DBAnggota();
    private String id, nama, alamat, ttl, kelamin, telp, pekerjaan, agama, iuran,action;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
//    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
//    throws ServletException, IOException {
//        response.setContentType("text/html;charset=UTF-8");
//        PrintWriter out = response.getWriter();
//        try {
//            /* TODO output your page here. You may use following sample code. */
//            out.println("<html>");
//            out.println("<head>");
//            out.println("<title>Servlet AnggotaServlet</title>");  
//            out.println("</head>");
//            out.println("<body>");
//            out.println("<h1>Servlet AnggotaServlet at " + request.getContextPath () + "</h1>");
//            out.println("</body>");
//            out.println("</html>");
//        } finally { 
//            out.close();
//        }
//    } 
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        processRequest(request, response);
         action = request.getParameter("action");
        id = request.getParameter("id");
        if (action.equals("edit")) {
            try {
                //get userdetails of particular name
                AnggotaBean ubean = dbobject.getAnggotaDetails(id);
                request.setAttribute("updateuser", ubean);
                RequestDispatcher rd = request.getRequestDispatcher("insert_anggota");
                rd.forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        if (action.equals("delete")) {
            try {
                //delete userdetails of particular name
                dbobject.deleteAnggota(id);
//                List list = dbobject.getAlldetails();
//                request.setAttribute("list", list);
                RequestDispatcher rd = request.getRequestDispatcher("view_anggota");
                rd.forward(request, response);
            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
//        processRequest(request, response);
         PrintWriter out = response.getWriter();
        id = request.getParameter("id");
        nama = request.getParameter("nama");
        alamat = request.getParameter("alamat");
        ttl = request.getParameter("ttl");
        kelamin = request.getParameter("kelamin");
        telp = request.getParameter("telepon");
        pekerjaan = request.getParameter("pekerjaan");
        agama = request.getParameter("agama");
        iuran = request.getParameter("iuran");
        action = request.getParameter("action");

        if (action.equals("Insert")) {
            try {
                setValuesToBean();
                dbobject.insertAnggota(bean);
out.print("<script>window.alert('Penambahan anggota sukses');window.location=('insert_anggota')</script>");

            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
         if (action.equals("Update")) {
            try {
                setValuesToBean();

                //using DBClass object call insertDetails method and pass bean object
                dbobject.UpdateAnggota(bean, id);

                out.print("<script>window.alert('Perubahan data anggota sukses');window.location=('view_anggota')</script>");

            } catch (SQLException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(PegawaiServlet.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }

    public void setValuesToBean() {
       // bean.setAnggotaId(id);
        bean.setNama(nama);
        bean.setAlamat(alamat);
        bean.setTanggalLahir(ttl);
        bean.setKelamin(kelamin);
        bean.setTelp(telp);
        bean.setPekerjaan(pekerjaan);
        bean.setAgama(agama);
        bean.setIuran(iuran);
    }
    public void notif(){
        
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
